package com.report.pdf;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.URLUtil;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.rendering.PDFRenderer;


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Iterator;
import java.util.List;

/**
 * @author summer
 * @date 2024-07-03 19:47
 */
public class PdfUtils {

    public static Integer getPages(String path) throws IOException {
        PDDocument pdDocument = getPDDocument(path);
        int numberOfPages = pdDocument.getNumberOfPages();
        pdDocument.close();
        return numberOfPages;
    }

    public static Integer getPages(InputStream inputStream) throws IOException {
        PDDocument pdDocument = PDDocument.load(inputStream);
        int numberOfPages = pdDocument.getNumberOfPages();
        pdDocument.close();
        inputStream.close();
        return numberOfPages;
    }

    private static PDDocument getPDDocument(String path) throws IOException {
        InputStream stream = URLUtil.getStream(new URL(path));
        return PDDocument.load(stream);
    }


    public static String getImage(InputStream inputStream, String tempFile) throws IOException {
        PDDocument pdDocument = PDDocument.load(inputStream);
        String image = getImage(pdDocument, tempFile);
        inputStream.close();
        return image;
    }

    private static String getImage(PDDocument pdDocument,String tempFile) throws IOException {
        PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
        String image="";
        for (int i = 0; i < pdDocument.getNumberOfPages(); i++) {
            image=tempFile+ IdUtil.nanoId()+".PNG";
            BufferedImage bufferedImage = pdfRenderer.renderImage(i);
            ImageIO.write(bufferedImage, "PNG", FileUtil.file(image));
            break;
        }
        pdDocument.close();
        return image;
    }

    /**
     * 获取pdf中的图片转inputStream
     * @param path
     * @return
     * @throws IOException
     */
    public  static String  getImage(String path, String tempFile) throws IOException {
        List<String> list = ListUtil.list(true) ;
        PDDocument pdDocument = getPDDocument(path);
        return getImage(pdDocument,tempFile);
    }

    public static String convertToBase64(String fileUrl) throws Exception {
        URL url = new URL(fileUrl);
        InputStream inputStream = url.openStream();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, bytesRead);
        }

        byte[] fileBytes = byteArrayOutputStream.toByteArray();
        String base64String = Base64.getEncoder().encodeToString(fileBytes);
        return "data:application/pdf;base64,"+base64String;
    }

 }
